package com.czz.vod.controller;

import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.vod.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName: VideoTeacherController
 * @Author Czz
 * @Date 2020/1/29
 * @Time 11:11
 **/
@RestController
@RequestMapping(value = "/teacher/video")
public class VideoTeacherController {

    @Autowired
    VideoService videoService;

    /**
    * @Description 讲师上传课时视频
    * @Param [file]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/29
    * @Time 16:08
    **/
    @PostMapping(value = "/upload")
    public R uploadVideo(@RequestParam("file") MultipartFile file){
        String videoId = videoService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("videoId", videoId);
    }

    /**
    * @Description 根据视频id删除阿里云中视频
    * @Param [videoId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/30
    * @Time 18:24
    **/
    @DeleteMapping(value = "/{videoId}")
    public R deleteVideoById(@PathVariable String videoId){
        if (StringUtils.isEmpty(videoId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        videoService.deleteVideo(videoId);
        return R.ok().message("视频删除成功");
    }

    /**
    * @Description 根据视频id列表批量删除阿里云中视频
    * @Param [videoList]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/1
    * @Time 19:55
    **/
    @DeleteMapping(value = "/delete-batch")
    public R deleteVideoBatch(@RequestBody List<String> videoList){
        if (CollectionUtils.isEmpty(videoList)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        videoService.deleteVideoBatch(videoList);
        return R.ok().message("批量视频删除成功");
    }

    /**
    * @Description 获取视频上传地址和凭证
    * @Param [title, fileName] 视频标题/视频文件名
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/2
    * @Time 11:12
    **/
    @GetMapping(value = "/get-upload-auth/{title}/{fileName}")
    public R getUploadAuth(@PathVariable String title,@PathVariable String fileName){
        if (StringUtils.isEmpty(title) || StringUtils.isEmpty(fileName)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        CreateUploadVideoResponse response = videoService.getUploadAuthAndAddress(title, fileName);
        return R.ok().message("获取视频上传地址和凭证成功").data("response", response);
    }

    /**
    * @Description 刷新视频上传地址和凭证
    * @Param [videoId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/2
    * @Time 11:15
    **/
    @GetMapping(value = "/refresh-upload-auth/{videoId}")
    public R refreshUploadAuth(@PathVariable String videoId){
        if (StringUtils.isEmpty(videoId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        RefreshUploadVideoResponse response = videoService.refreshUploadAuthAndAddress(videoId);
        return R.ok().message("刷新视频上传地址和凭证成功").data("response", response);
    }
}
